package NewTmas.FramesandPanels;

import NewTmas.Listeners.ColumnShowListener;
import taskmaster.auth.User;
import taskmaster.job.QueryParams;
import taskmaster.job.TaskBuilder;
import taskmaster.job.TaskCollection;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 7/7/11
 * Time: 1:35 PM
 * To change this template use File | Settings | File Templates.
 */
public class JobStates extends JInternalFrame
{
    String jobName;
    User user;
    Date date;
    TaskBuilder tb;
    JDesktopPane desktopPane;
    TablePanel tp;
    JMenuItem menuItem1, menuItem2;
    QueryParams limiters;
    TaskCollection tc = null;

    public JobStates(final User user, final Date date, final TaskBuilder tb, final JDesktopPane dtp, final HashMap<String, String> signoffMap, Hashtable allRolesHash)
    {

        this.tp = new TablePanel(user, date, tb, allRolesHash);
        this.user = user;
        this.tb = tb;
        this.date = date;
        this.desktopPane = dtp;

        JTabbedPane tabbedPane = new JTabbedPane();
        add(tabbedPane);
        JPanel p = tp.getJobTablePanel();
        tabbedPane.addTab("Job States", new ImageIcon(JobStates.class.getResource("/Resources/Icons/table.png")), p, "click to view the Job States table");

        setTitle("Job States: " + date);
        toFront();
        setVisible(true);
        setIconifiable(true);
        setClosable(true);
        setResizable(true);
        setMaximizable(true);
        setSize(400, 400);


        final JPopupMenu popup = new JPopupMenu();
        JMenu viewGraphMenu = new JMenu("View job graph with ");
        menuItem1 = new JMenuItem("JGraphX");
        menuItem2 = new JMenuItem("GraphViz");
        viewGraphMenu.add(menuItem1);
        viewGraphMenu.add(menuItem2);
        popup.add(viewGraphMenu);

        menuItem1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                //     IntFrame nf = new IntFrame(new String[]{jobName},date,new String[0],new int[0],"JGraphX",user, tb,dtp);
                limiters = new QueryParams(new int[0], new String[]{jobName}, new HashSet<String>(), new char[0], date, true);


                try
                {
                    tc = tb.getTaskCollection(limiters, user);
                } catch (SQLException ex)
                {
                    ex.printStackTrace();
                    throw new RuntimeException(ex);
                }

                IntFrame nf = new IntFrame(tc, limiters, date, "JGraphX", user, tb, dtp, signoffMap, 0, 0);

            }
        });

        menuItem2.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                //     IntFrame nf = new IntFrame(new String[]{jobName},date,new String[0],new int[0],"GraphViz",user, tb,dtp);
                limiters = new QueryParams(new int[0], new String[]{jobName}, new HashSet<String>(), new char[0], date, true);

                try
                {
                    tc = tb.getTaskCollection(limiters, user);
                } catch (SQLException ex)
                {
                    ex.printStackTrace();
                    throw new RuntimeException(ex);
                }

                JComboBox XDim = new JComboBox();
                JComboBox YDim = new JComboBox();
                for (int i : JobMonitorOpenerFrame.xdim)
                {
                    XDim.addItem(i);
                }

                for (int i : JobMonitorOpenerFrame.ydim)
                {

                    YDim.addItem(i);
                }

                Object message = new Object[]{"X dimension", XDim, "Y Dimension", YDim};


                JOptionPane.showConfirmDialog(null, message, "Select Graph dimensions", JOptionPane.DEFAULT_OPTION);
                int graphWidth = Integer.parseInt(XDim.getSelectedItem().toString());
                int graphHeight = Integer.parseInt(YDim.getSelectedItem().toString());

                IntFrame nf = new IntFrame(tc, limiters, date, "GraphViz", user, tb, dtp, signoffMap, graphWidth, graphHeight);

            }
        });

        tp.tcTable.addMouseListener(new MouseAdapter()
        {

            public void mousePressed(MouseEvent e) {Check(e);}

            public void mouseReleased(MouseEvent e) { Check(e); }

            public void Check(MouseEvent e)
            {
                if (e.isPopupTrigger())
                {
                    JTable source = (JTable) e.getSource();
                    int row = source.rowAtPoint(e.getPoint());
                    int column = source.columnAtPoint(e.getPoint());

                    if (!source.isRowSelected(row))
                    {
                        source.changeSelection(row, column, false, false);
                    }
                    Point pt = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), source);
                    popup.show(source, pt.x, pt.y);
                    jobName = source.getModel().getValueAt(source.convertRowIndexToModel(row), 0).toString();


                }
            }
        });


        JMenuBar menubar = new JMenuBar();
        setJMenuBar(menubar);
        JMenu actions = new JMenu("Actions");
        menubar.add(actions);
        JMenuItem refresh = new JMenuItem("refresh");
        actions.add(refresh);
        refresh.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                tp.refreshJobStatesTablePanel();
            }
        });

        JMenu showColumns = new JMenu("Show Columns");

        for (int i = 0; i < tp.tcTable.getColumnModel().getColumnCount(); i++)
        {
            JCheckBoxMenuItem item;
            showColumns.add(item = new JCheckBoxMenuItem(tp.tcTable.getColumnName(i), true));
            item.addItemListener(new ColumnShowListener(tp, i));
        }
        menubar.add(showColumns);

    }


}
